<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"c/io/fprintf","wgTitle":"c/io/fprintf","wgCurRevisionId":54105,"wgArticleId":972,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"c/io/fprintf","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-c {line-height: normal;}
.source-c li, .source-c pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for c
 * CSS class: source-c, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.c.source-c .de1, .c.source-c .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.c.source-c  {font-family:monospace;}
.c.source-c .imp {font-weight: bold; color: red;}
.c.source-c li, .c.source-c .li1 {font-weight: normal; vertical-align:top;}
.c.source-c .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.c.source-c .li2 {font-weight: bold; vertical-align:top;}
.c.source-c .kw1 {color: #0000dd;}
.c.source-c .kw2 {color: #0000ff;}
.c.source-c .kw3 {color: #0000dd;}
.c.source-c .kw4 {color: #0000ff;}
.c.source-c .co1 {color: #909090;}
.c.source-c .co2 {color: #339900;}
.c.source-c .coMULTI {color: #ff0000; font-style: italic;}
.c.source-c .es0 {color: #008000; font-weight: bold;}
.c.source-c .es1 {color: #008000; font-weight: bold;}
.c.source-c .es2 {color: #008000; font-weight: bold;}
.c.source-c .es3 {color: #008000; font-weight: bold;}
.c.source-c .es4 {color: #008000; font-weight: bold;}
.c.source-c .es5 {color: #008000; font-weight: bold;}
.c.source-c .br0 {color: #008000;}
.c.source-c .sy0 {color: #008000;}
.c.source-c .sy1 {color: #000080;}
.c.source-c .sy2 {color: #000040;}
.c.source-c .sy3 {color: #000040;}
.c.source-c .sy4 {color: #008080;}
.c.source-c .st0 {color: #008000;}
.c.source-c .nu0 {color: #000080;}
.c.source-c .nu6 {color:#000080;}
.c.source-c .nu8 {color:#000080;}
.c.source-c .nu12 {color:#000080;}
.c.source-c .nu16 {color:#000080;}
.c.source-c .nu17 {color:#000080;}
.c.source-c .nu18 {color:#000080;}
.c.source-c .nu19 {color:#000080;}
.c.source-c .ln-xtra, .c.source-c li.ln-xtra, .c.source-c div.ln-xtra {background-color: #ffc;}
.c.source-c span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_io_fprintf skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../c.html" title="c">c</a>‎ | <a href="../io.html" title="c/io">io</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../c.html" title="c"> C</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="c/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="c/header">头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../types.html" title="c/types">类型支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../program.html" title="c/program">程序工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../variadic.html" title="c/variadic">变参数函数支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../memory.html" title="c/memory">动态内存管理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="c/error">错误处理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../chrono.html" title="c/chrono">日期和时间工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="c/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="c/algorithm">算法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="c/numeric">数值</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="c/io">输入/输出支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="c/locale">本地化支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="c/thread">线程支持</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="c/atomic">原子操作</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../io.html" title="c/io"> 文件输入/输出</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h1"><td colspan="5"> 函数</td></tr>
<tr class="t-nv-h2"><td colspan="5"> 文件访问</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fopen.html" title="c/io/fopen"><span class="t-lines"><span>fopen</span><span>fopen_s</span></span></a></div><div><span class="t-lines"><span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="freopen.html" title="c/io/freopen"><span class="t-lines"><span>freopen</span><span>freopen_s</span></span></a></div><div><span class="t-lines"><span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="https://zh.cppreference.com/w/c/io/fwide" title="c/io/fwide"><span class="t-lines"><span>fwide</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div></div></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="setbuf.html" title="c/io/setbuf"><span class="t-lines"><span>setbuf</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="setvbuf.html" title="c/io/setvbuf"><span class="t-lines"><span>setvbuf</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fclose.html" title="c/io/fclose"><span class="t-lines"><span>fclose</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fflush.html" title="c/io/fflush"><span class="t-lines"><span>fflush</span></span></a></div></div></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 直接输入/输出</td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fread.html" title="c/io/fread"><span class="t-lines"><span>fread</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fwrite.html" title="c/io/fwrite"><span class="t-lines"><span>fwrite</span></span></a></div></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 无格式输入/输出</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fgetc.html" title="c/io/fgetc"><span class="t-lines"><span>fgetc</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fgets.html" title="c/io/fgets"><span class="t-lines"><span>fgets</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fputc.html" title="c/io/fputc"><span class="t-lines"><span>fputc</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fputs.html" title="c/io/fputs"><span class="t-lines"><span>fputs</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="getchar.html" title="c/io/getchar"><span class="t-lines"><span>getchar</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="gets.html" title="c/io/gets"><span class="t-lines"><span>gets</span><span>gets_s</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-until-c11">(C11 前)</span></span><span><span class="t-mark-rev t-since-c11">(C11 起)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="putchar.html" title="c/io/putchar"><span class="t-lines"><span>putchar</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="puts.html" title="c/io/puts"><span class="t-lines"><span>puts</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="ungetc.html" title="c/io/ungetc"><span class="t-lines"><span>ungetc</span></span></a></div></div></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fgetwc.html" title="c/io/fgetwc"><span class="t-lines"><span>fgetwc</span><span>getwc</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fgetws.html" title="c/io/fgetws"><span class="t-lines"><span>fgetws</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fputwc.html" title="c/io/fputwc"><span class="t-lines"><span>fputwc</span><span>putwc</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fputws.html" title="c/io/fputws"><span class="t-lines"><span>fputws</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="getwchar.html" title="c/io/getwchar"><span class="t-lines"><span>getwchar</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="putwchar.html" title="c/io/putwchar"><span class="t-lines"><span>putwchar</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="ungetwc.html" title="c/io/ungetwc"><span class="t-lines"><span>ungetwc</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div></div></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 有格式输入</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fscanf.html" title="c/io/fscanf"><span class="t-lines"><span>scanf</span><span>fscanf</span><span>sscanf</span><span>scanf_s</span><span>fscanf_s</span><span>sscanf_s</span></span></a></div><div><span class="t-lines"><span></span><span></span><span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fwscanf.html" title="c/io/fwscanf"><span class="t-lines"><span>wscanf</span><span>fwscanf</span><span>swscanf</span><span>wscanf_s</span><span>fwscanf_s</span><span>swscanf_s</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="vfscanf.html" title="c/io/vfscanf"><span class="t-lines"><span>vscanf</span><span>vfscanf</span><span>vsscanf</span><span>vscanf_s</span><span>vfscanf_s</span><span>vsscanf_s</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="vfwscanf.html" title="c/io/vfwscanf"><span class="t-lines"><span>vwscanf</span><span>vfwscanf</span><span>vswscanf</span><span>vwscanf_s</span><span>vfwscanf_s</span><span>vswscanf_s</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 有格式输出</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><strong class="selflink"><span class="t-lines"><span>printf</span><span>fprintf</span><span>sprintf</span><span>snprintf</span><span>printf_s</span><span>fprintf_s</span><span>sprintf_s</span><span>snprintf_s</span></span></strong></div><div><span class="t-lines"><span></span><span></span><span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fwprintf.html" title="c/io/fwprintf"><span class="t-lines"><span>wprintf</span><span>fwprintf</span><span>swprintf</span><span>wprintf_s</span><span>fwprintf_s</span><span>swprintf_s</span><span>snwprintf_s</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="vfprintf.html" title="c/io/vfprintf"><span class="t-lines"><span>vprintf</span><span>vfprintf</span><span>vsprintf</span><span>vsnprintf</span><span>vprintf_s</span><span>vfprintf_s</span><span>vsprintf_s</span><span>vsnprintf_s</span></span></a></div><div><span class="t-lines"><span></span><span></span><span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="vfwprintf.html" title="c/io/vfwprintf"><span class="t-lines"><span>vwprintf</span><span>vfwprintf</span><span>vswprintf</span><span>vwprintf_s</span><span>vfwprintf_s</span><span>vswprintf_s</span><span>vsnwprintf_s</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 文件寻位</td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="ftell.html" title="c/io/ftell"><span class="t-lines"><span>ftell</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fgetpos.html" title="c/io/fgetpos"><span class="t-lines"><span>fgetpos</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fseek.html" title="c/io/fseek"><span class="t-lines"><span>fseek</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="fsetpos.html" title="c/io/fsetpos"><span class="t-lines"><span>fsetpos</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="rewind.html" title="c/io/rewind"><span class="t-lines"><span>rewind</span></span></a></div></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 错误处理</td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="clearerr.html" title="c/io/clearerr"><span class="t-lines"><span>clearerr</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="feof.html" title="c/io/feof"><span class="t-lines"><span>feof</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="ferror.html" title="c/io/ferror"><span class="t-lines"><span>ferror</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="perror.html" title="c/io/perror"><span class="t-lines"><span>perror</span></span></a></div></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 文件上的操作</td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="remove.html" title="c/io/remove"><span class="t-lines"><span>remove</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="rename.html" title="c/io/rename"><span class="t-lines"><span>rename</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="tmpfile.html" title="c/io/tmpfile"><span class="t-lines"><span>tmpfile</span><span>tmpfile_s</span></span></a></div><div><span class="t-lines"><span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="tmpnam.html" title="c/io/tmpnam"><span class="t-lines"><span>tmpnam</span><span>tmpnam_s</span></span></a></div><div><span class="t-lines"><span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<table class="t-dcl-begin"><tbody>
<tr class="t-dsc-header">
<td> <div>定义于头文件 <code>&lt;stdio.h&gt;</code>
 </div></td>
<td></td>
<td></td>
</tr>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num"><tr class="t-dcl-rev-aux">
<td></td>
<td rowspan="142">(1)</td>
<td></td>
</tr>
<tr class="t-dcl t-until-c99">
<td> <div><span class="mw-geshi c source-c">​<span class="kw4">int</span> printf<span class="br0">(</span> <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span>format, ... <span class="br0">)</span><span class="sy4">;</span>​</span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-until-c99">(C99 前)</span> </td>
</tr>
<tr class="t-dcl t-since-c99">
<td> <div><span class="mw-geshi c source-c">​<span class="kw4">int</span> printf<span class="br0">(</span> <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> format, ... <span class="br0">)</span><span class="sy4">;</span>​</span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-since-c99">(C99 起)</span> </td>
</tr>
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num"><tr class="t-dcl-rev-aux">
<td></td>
<td rowspan="142">(2)</td>
<td></td>
</tr>
<tr class="t-dcl t-until-c99">
<td> <div><span class="mw-geshi c source-c"><span class="kw4">int</span> fprintf<span class="br0">(</span> <a href="../io.html"><span class="kw880">FILE</span></a> <span class="sy2">*</span>stream, <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span>format, ... <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-until-c99">(C99 前)</span> </td>
</tr>
<tr class="t-dcl t-since-c99">
<td> <div><span class="mw-geshi c source-c"><span class="kw4">int</span> fprintf<span class="br0">(</span> <a href="../io.html"><span class="kw880">FILE</span></a> <span class="sy2">*</span><span class="kw4">restrict</span> stream, <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> format, ... <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-since-c99">(C99 起)</span> </td>
</tr>
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num"><tr class="t-dcl-rev-aux">
<td></td>
<td rowspan="142">(3)</td>
<td></td>
</tr>
<tr class="t-dcl t-until-c99">
<td> <div><span class="mw-geshi c source-c"><span class="kw4">int</span> sprintf<span class="br0">(</span> <span class="kw4">char</span> <span class="sy2">*</span>buffer, <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span>format, ... <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-until-c99">(C99 前)</span> </td>
</tr>
<tr class="t-dcl t-since-c99">
<td> <div><span class="mw-geshi c source-c"><span class="kw4">int</span> sprintf<span class="br0">(</span> <span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> buffer, <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> format, ... <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-since-c99">(C99 起)</span> </td>
</tr>
</tbody><tbody>
<tr class="t-dcl t-since-c99">
<td> <div><span class="mw-geshi c source-c"><span class="kw4">int</span> snprintf<span class="br0">(</span> <span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> buffer, <span class="kw4">int</span> bufsz, <br>
              <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> format, ... <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td> (4) </td>
<td> <span class="t-mark-rev t-since-c99">(C99 起)</span> </td>
</tr>
<tr class="t-dcl t-since-c11">
<td> <div><span class="mw-geshi c source-c"><span class="kw4">int</span> printf_s<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> format, ...<span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td> (5) </td>
<td> <span class="t-mark-rev t-since-c11">(C11 起)</span> </td>
</tr>
<tr class="t-dcl t-since-c11">
<td> <div><span class="mw-geshi c source-c"><span class="kw4">int</span> fprintf_s<span class="br0">(</span><a href="../io.html"><span class="kw880">FILE</span></a> <span class="sy2">*</span><span class="kw4">restrict</span> stream, <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> format, ...<span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td> (6) </td>
<td> <span class="t-mark-rev t-since-c11">(C11 起)</span> </td>
</tr>
<tr class="t-dcl t-since-c11">
<td> <div><span class="mw-geshi c source-c"><span class="kw4">int</span> sprintf_s<span class="br0">(</span><span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> buffer, rsize_t bufsz,<br>
              <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> format, ...<span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td> (7) </td>
<td> <span class="t-mark-rev t-since-c11">(C11 起)</span> </td>
</tr>
<tr class="t-dcl t-since-c11">
<td> <div><span class="mw-geshi c source-c"><span class="kw4">int</span> snprintf_s<span class="br0">(</span><span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> buffer, rsize_t bufsz,<br>
               <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span><span class="kw4">restrict</span> format, ...<span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td> (8) </td>
<td> <span class="t-mark-rev t-since-c11">(C11 起)</span> </td>
</tr>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>从给定位置加载数据，转换为字符串等价物，并写结果到各种池。
</p>
<div class="t-li1"><span class="t-li">1)</span> 写结果到 <span class="t-lc"><a href="../io.html" title="c/io">stdout</a></span> 。</div>
<div class="t-li1"><span class="t-li">2)</span> 写结果到文件流 <code>stream</code> 。</div>
<div class="t-li1"><span class="t-li">3)</span> 写结果到字符串 <code>buffer</code> 。</div>
<div class="t-li1"><span class="t-li">4)</span> 写结果到字符串 <code>buffer</code> 。至多写 <code>buf_size</code> - 1 个字符。产生的字符串会以空字符终止，除非 <code>buf_size</code> 为零。若 <code>buf_size</code> 为零，则不写入任何内容，且 <code>buffer</code> 可以是空指针，然而依旧计算返回值（会写入的字符数，不包含空终止符）并返回。</div>
<div class="t-li1"><span class="t-li">5-8)</span> 同 <span class="t-v">(1-4)</span> ，除了在运行时检测下列错误，并调用当前安装的<a href="../error/set_constraint_handler_s.html" title="c/error/set constraint handler s">制约处理</a>函数：
<dl><dd><ul><li>  <code>format</code> 中存在转换指定符 <code>%n</code>
</li><li>  任何一个对应 <code>%s</code> 的参数是空指针
</li><li>  <code>format</code> 或 <code>buffer</code> 是空指针
</li><li>  <code>bufsz</code> 为零或大于 <span class="t-lc">RSIZE_MAX</span>
</li><li>  在任何一个字符串及字符转换指定符中出现编码错误
</li><li>  （仅对于 <code>sprintf_s</code> ）存储于 <code>buffer</code> 的字符串（包括尾随空字符）长度将超出 <code>bufsz</code>
</li></ul>
</dd><dd>
</dd><dd>同所有边界检查函数， <code>printf_s, fprintf_s, sprintf_s, snrintf_s</code> 仅若实现定义了 <span class="t-c"><span class="mw-geshi c source-c">__STDC_LIB_EXT1__</span></span> ，且用户在包含 <code>&lt;stdio.h&gt;</code> 前定义 <span class="t-c"><span class="mw-geshi c source-c">__STDC_WANT_LIB_EXT1__</span></span> 为整数常量 <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">1</span></span></span> 才保证可用。</dd></dl></div>

<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">参数</span></h3>
<table class="t-par-begin">


<tr class="t-par">
<td>  stream
</td>
<td> -
</td>
<td>  要写入的输出文件流
</td></tr>
<tr class="t-par">
<td>  buffer
</td>
<td> -
</td>
<td>  指向要写入的字符串的指针
</td></tr>
<tr class="t-par">
<td>  bufsz
</td>
<td> -
</td>
<td>  最多会写入 bufsz - 1 个字符，再加空终止符
</td></tr>
<tr class="t-par">
<td>  format
</td>
<td> -
</td>
<td>  指向指定数据转译方式的空终止多字节字符串的指针。
<p>格式字符串由普通多字节字符（除了 <code><b>%</b></code> ）和转换指定构成，前者被复制到输出流而无更改。每个转换指定拥有下列格式：
</p>
<dl><dd><ul><li> 引入的 <code><b>%</b></code> 字符
</li></ul>
</dd></dl>
<dl><dd><ul><li> <span class="t-mark">(可选)</span>一或多个修改转换行为的标签：
</li></ul>
<dl><dd><ul><li> <code><b>-</b></code> ：转换结果在域内左校正（默认为右校正）
</li><li> <code><b>+</b></code> ：有符号转换的符号始终前置于转换结果（默认结果前置负号仅当它为负）
</li><li> <i>空格</i>：若有符号转换的结果不以符号开始，或为空，则前置空格于结果。若存在 <code>+</code> 标签则忽略<i>空格</i>。
</li><li> <code><b>#</b></code> ：进行<i>替用形式</i>的转换。准确的效果见下表，其他情况下行为未定义。
</li><li> <code><b>0</b></code> ：对于整数和浮点数转换，使用前导零代替<i>空格</i>字符填充域。对于整数，若显式指定精度，则忽略此标签。对于其他转换，使用此标签导致未定义行为。若存在 <code>-</code> 标签则忽略 <code>0</code> 。
</li></ul>
</dd></dl>
</dd></dl>
<dl><dd><ul><li> <span class="t-mark">(可选)</span>指定最小域宽的整数值或 <code>*</code> 。若有要求，则结果会以<i>空格</i>字符（默认情况）填充，在右校正时于左，左校正时于右。使用 <code>*</code> 的情况下，以一个额外的 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">int</span></span></span> 类型参数指定宽度。若参数值为负数，则它导致指定 <code>-</code> 标签和正域宽。（注意：这是最小宽度：决不被截断值）。
</li></ul>
</dd></dl>
<dl><dd><ul><li> <span class="t-mark">(可选)</span>后随整数或 <code><b>*</b></code> 或两者皆无的 <code><b>.</b></code> 指示转换的<i>精度</i>。在使用 <code>*</code> 的情况下，<i>精度</i>由额外的 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">int</span></span></span> 类型参数指定。若此参数的值为负数，则它被忽略。若既不使用数字亦不使用 <code><b>*</b></code> ，则精度采用零。<i>精度</i>的准确效果见下表。 
</li></ul>
</dd></dl>
<dl><dd><ul><li> <span class="t-mark">(可选)</span>指定参数大小的<i>长度修饰符</i>
</li></ul>
</dd></dl>
<dl><dd><ul><li> 转换格式指定符
</li></ul>
</dd></dl>
<p>下列格式指定符可用：
</p>
<table class="wikitable" style="text-align:center; font-size:85%; max-width:90em;">

<tr>
<th rowspan="1"> 转换<br>指定符
</th>
<th rowspan="1"> 解释
</th>
<th colspan="9"> 参数类型
</th></tr>
<tr>
<th colspan="2" style="text-align:right; font-weight:normal;"> 长度修饰符
</th>
<th> <code>hh</code>
<p><span class="t-mark-rev t-since-c99">(C99)</span>
</p>
</th>
<th> <code>h</code>
</th>
<th style="font-weight:normal;"> （无）
</th>
<th> <code><b>l</b></code>
</th>
<th> <code><b>ll</b></code>
<p><span class="t-mark-rev t-since-c99">(C99)</span>
</p>
</th>
<th> <code><b>j</b></code>
<p><span class="t-mark-rev t-since-c99">(C99)</span>
</p>
</th>
<th> <code><b>z</b></code>
<p><span class="t-mark-rev t-since-c99">(C99)</span>
</p>
</th>
<th> <code><b>t</b></code>
<p><span class="t-mark-rev t-since-c99">(C99)</span>
</p>
</th>
<th> <code><b>L</b></code>
</th></tr>
<tr>
<th> <code>%</code>
</th>
<td style="text-align:left;"> 写字面的 <code><b>%</b></code> 。完整转换指定必须是 <code><b>%%</b></code> 。
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>c</code>
</th>
<td style="text-align:left;">
<dl><dd> 写<b>单个字符</b>。
</dd></dl>
<p>参数首先被转换成 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">char</span></span></span> 。
若使用 <b>l</b> 修饰符，则参数首先被转换成字符串，如同通过以 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">wchar_t</span><span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span></span></span> 参数使用 <b>%ls</b> 。
</p>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">int</span></span></span></div></div>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c">wint_t</span></span></div></div>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>s</code>
</th>
<td style="text-align:left;">
<dl><dd> 写<b>字符串</b>
</dd></dl>
<p>参数必须是指向字符数组首元素的指针
<i>精度</i>指定写入最大的字符数。若未指定<i>精度</i>，则写每个字节直到而而不含首个空终止符。
若使用 <b>l</b> 指定符，则参数必须是指向 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">wchar_t</span></span></span> 数组首元素的指针，数组会被转换成 char 数组，如同通过以零初始化转换状态调用 <span class="t-lc"><a href="../string/multibyte/wcrtomb.html" title="c/string/multibyte/wcrtomb">wcrtomb</a></span> 。
</p>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">char</span><span class="sy2">*</span></span></span></div></div>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">wchar_t</span><span class="sy2">*</span></span></span></div></div>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>d</code><br> <code>i</code>
</th>
<td style="text-align:left;">
<dl><dd>转换<b>有符号整数</b>为十进制表示 <i>[-]dddd</i> 。
</dd></dl>
<p><i>精度</i>指定出现的最小数位数。默认精度是 <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">1</span></span></span> 。<br> 
若被转换的值和精度都是 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> ，则转换结果无字符。
</p>
</td>
<td rowspan="1"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">signed</span> <span class="kw4">char</span></span></span></div></div>
</td>
<td rowspan="1"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">short</span></span></span></div></div>
</td>
<td rowspan="1"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">int</span></span></span></div></div>
</td>
<td rowspan="1"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">long</span></span></span></div></div>
</td>
<td rowspan="1"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">long</span> <span class="kw4">long</span></span></span></div></div>
</td>
<td rowspan="1"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><a href="../types/integer.html"><span class="kw118">intmax_t</span></a></span></span></div></div>
</td>
<td rowspan="1"> <div class="t-vertical"><div>有符号 <span class="t-c"><span class="mw-geshi c source-c"><a href="../types/size_t.html"><span class="kw100">size_t</span></a></span></span></div></div>
</td>
<td rowspan="1"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><a href="../types/ptrdiff_t.html"><span class="kw101">ptrdiff_t</span></a></span></span></div></div>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>o</code>
</th>
<td style="text-align:left;">
<dl><dd>转换<b>无符号整数</b>为八进制表示 <i>oooo</i> 。
</dd></dl>
<p><i>精度</i>指定出现的最小数位数。默认精度是 <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">1</span></span></span> 。 
若被转换值和精度都是 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> ，则转换结果无字符。
在<i>替用实现</i>中精度按需增加，以写入一个前导零。
在此情况下若被转换值和精度都是 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> ，则写入单个 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> 。
</p>
</td>
<td rowspan="3"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">char</span></span></span></div></div>
</td>
<td rowspan="3"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">short</span></span></span></div></div>
</td>
<td rowspan="3"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">int</span></span></span></div></div>
</td>
<td rowspan="3"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">long</span></span></span></div></div>
</td>
<td rowspan="3"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">long</span> <span class="kw4">long</span></span></span></div></div>
</td>
<td rowspan="3"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><a href="../types/integer.html"><span class="kw132">uintmax_t</span></a></span></span></div></div>
</td>
<td rowspan="3"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><a href="../types/size_t.html"><span class="kw100">size_t</span></a></span></span></div></div>
</td>
<td rowspan="3"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><a href="../types/ptrdiff_t.html"><span class="kw101">ptrdiff_t</span></a></span></span> 的无符号版本</div></div>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>x</code><br> <code>X</code>
</th>
<td style="text-align:left;">
<dl><dd>转换<b>无符号整数</b>为十六进制表示 <i>hhhh</i> 。
</dd></dl>
<p><code><b>x</b></code> 转换使用字母 <code>abcdef</code> 。<br>
<code><b>X</b></code> 转换使用 <code>ABCDEF</code> 。<br>
<i>精度</i>指定出现的最小数位数。默认精度是 <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">1</span></span></span> 。 
若被转换值和精度都是 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> ，则转换结果无字符。
在<i>替用实现中</i>若被转换值为非零则结果有 <code><b>0x</b></code> 或 <code><b>0X</b></code> 前缀。
</p>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>u</code>
</th>
<td style="text-align:left;">
<dl><dd>转换<b>无符号整数</b>为十进制表示 <i>dddd</i> 。
</dd></dl>
<p><i>精度</i>指定出现的最小数位数。
默认精度是 <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">1</span></span></span> 。 
若被转换值和精度都是 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> ，则转换结果无字符。
</p>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>f</code><br><code>F</code>
</th>
<td style="text-align:left;">
<dl><dd>转换<b>浮点数</b>为 <i>[-]ddd.ddd</i> 样式的十进制记法。
</dd></dl>
<p><i>精度</i>指定小数点字符后出现的准确数位数。
默认精度是 <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">6</span></span></span> 。
在<i>替用实现</i>中即使没有小数点后数位也写小数点。
无穷大和非数的转换样式见注意。
</p>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td rowspan="4"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">double</span></span></span></div></div>
</td>
<td rowspan="4"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">double</span></span></span> <span class="t-mark-rev t-since-c99">(C99)</span></div></div>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td rowspan="4"> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">long</span> <span class="kw4">double</span></span></span></div></div>
</td></tr>
<tr>
<th> <code>e</code> <br> <code>E</code>
</th>
<td style="text-align:left;">
<dl><dd>转换<b>浮点数</b>为十进制指数记法。
</dd></dl>
<p><code><b>e</b></code> 转换样式使用 <i>[-]d.ddd</i><code><b>e</b></code><i>±dd</i> 。<br>
<code><b>E</b></code> 转换样式使用 <i>[-]d.ddd</i><code><b>E</b></code><i>±dd</i> 。<br>
指数至少含二个数位，仅当所需时使用更多数位。
若值为 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> ，则指数亦为 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> 。
<i>精度</i>指定小数点字符后出现的最小数位数。
默认精度是 <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">6</span></span></span> 。
在<i>替用实现</i>中即使没有小数点后数位也写小数点。
无穷大和非数的转换样式见注意。
</p>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>a</code> <br> <code>A</code>
<p><span class="t-mark-rev t-since-c99">(C99)</span>
</p>
</th>
<td style="text-align:left;">
<dl><dd>转换<b>浮点数</b>为十六进制记法。
</dd></dl>
<p><code><b>a</b></code> 转换样式使用 <i>[-]</i><code><b>0x</b></code><i>h.hhh</i><code><b>p</b></code><i>±d</i> 。<br>
<code><b>A</b></code> 转换样式使用 <i>[-]</i><code><b>0X</b></code><i>h.hhh</i><code><b>P</b></code><i>±d</i> 。<br>
若参数是正规化的浮点值，则首个十六进制数位非 <code>0</code> 。
若值为 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> ，则指数亦为 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> 。
<i>精度</i>指定小数点字符后出现的最小数位数。
默认精度足以准确表示该值。
在<i>替用实现</i>中即使没有小数点后数位也写小数点。
无穷大和非数的转换样式见注意。
</p>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>g</code> <br> <code>G</code>
</th>
<td style="text-align:left;">
<dl><dd>转换<b>浮点数</b>为十进制小数或十进制指数记法，依赖于值和<i>精度</i>。
</dd></dl>
<p><code><b>g</b></code> 转换样式将进行带样式 <code><b>e</b></code> 或 <code><b>f</b></code> 的转换。<br>
<code><b>G</b></code> 转换样式将进行带样式 <code><b>E</b></code> 或 <code><b>F</b></code> 的转换。<br>
令 <code>P</code> 等于精度，若它非零，若精度未指定则为 <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">6</span></span></span> ，若精度为 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> 则等于 <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">1</span></span></span> 。然后，若带样式 <code>E</code> 的转换会有指数 <code>X</code> ，则：
</p>
<ul><li> 若 <i>P &gt; X ≥ −4</i> ，则转换带 <code><b>f</b></code> 或 <code><b>F</b></code> 风格，及精度 <i>P − 1 − X</i> 。
</li><li> 否则，转换带 <code><b>e</b></code> 或 <code><b>E</b></code> 风格，及精度 <i>P − 1</i> 。
</li></ul>
<p>除非要求<i>替用表示</i>，否则末尾零会被移除，且若不留下小数部分则小数点亦被移除。
无穷大和非数的转换样式见注意。
</p>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>n</code>
</th>
<td style="text-align:left;">
<dl><dd>返回对函数的此调用迄今为止<b>写入的字符数</b>。
</dd></dl>
<p>结果被<i>写入</i>到参数所指向的值。 
该指定不可含有任何<i>标签</i>、<i>域宽</i>或<i>精度</i>。
</p>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">signed</span> <span class="kw4">char</span><span class="sy2">*</span></span></span></div></div>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">short</span><span class="sy2">*</span></span></span></div></div>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">int</span><span class="sy2">*</span></span></span></div></div>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">long</span><span class="sy2">*</span></span></span></div></div>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">long</span> <span class="kw4">long</span><span class="sy2">*</span></span></span></div></div>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><a href="../types/integer.html"><span class="kw118">intmax_t</span></a><span class="sy2">*</span></span></span></div></div>
</td>
<td> <div class="t-vertical"><div>有符号 <span class="t-c"><span class="mw-geshi c source-c"><a href="../types/size_t.html"><span class="kw100">size_t</span></a><span class="sy2">*</span></span></span></div></div>
</td>
<td> <div class="t-vertical"><div><span class="t-c"><span class="mw-geshi c source-c"><a href="../types/ptrdiff_t.html"><span class="kw101">ptrdiff_t</span></a><span class="sy2">*</span></span></span></div></div>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr>
<tr>
<th> <code>p</code>
</th>
<td style="text-align:left;"> 写定义<b>指针</b>的实现定义字符序列。
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">void</span><span class="sy2">*</span></span></span>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td>
<td style="background: #ececec; color: grey; vertical-align: middle; text-align: center;" class="table-na"> <small>N/A</small>
</td></tr></table>
<p>浮点转换函数转换无穷大到 <code>inf</code> 或 <code>infinity</code> 。使用哪个是实现定义的。
</p><p>非数转换成 <code>nan</code> 或 <code>nan(<i>char_sequence</i>)</code> 。使用哪个是实现定义的。
</p><p>转换 <code><b>F</b></code> 、 <code><b>E</b></code> 、 <code><b>G</b></code> 、 <code><b>A</b></code> 替代上面输出 <code>INF</code> 、 <code>INFINITY</code> 、 <code>NAN</code>。
</p><p>尽管 <code>%c</code> 期待 <code>int</code> 参数，传递 <code>char</code> 是安全的，因为在调用变参数函数时发生整数提升。
</p><p>定宽整数类型（ <span class="t-lc"><a href="../types/integer.html" title="c/types/integer">int8_t</a></span> 等）的正确转换指定定义于头文件 <a href="../types/integer.html" title="c/types/integer">&lt;inttypes.h&gt;</a> （尽管 <span class="t-lc"><a href="../types/integer.html" title="c/types/integer">PRIdMAX</a></span> 、 <span class="t-lc"><a href="../types/integer.html" title="c/types/integer">PRIuMAX</a></span> 等就是 <code>%jd</code> 、 <code>%ju</code> 等的别名）。
</p><p>内存写入转换指定符 <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">%</span>n</span></span> 是安全漏洞的常见目标，这里格式字符串依赖用户输入，而有边界检查的 <code>printf_s</code> 系列函数不支持此转换指定符。
</p><p>在每个转换指定符的行动后有一个<a href="../language/eval_order.html" title="c/language/eval order">序列点</a>；这允许于同一变量多次存入 <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">%</span>n</span></span> 的结果，并在同一此调用中打印出先前以 <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">%</span>n</span></span> 存储的值。
</p><p>若转换指定非法，则行为未定义。
</p><p><br>
</p>
</td></tr>
<tr class="t-par">
<td>  ...
</td>
<td> -
</td>
<td>  指定要打印数据的参数。若任何<a href="../language/conversion.html#.E9.BB.98.E8.AE.A4.E5.8F.82.E6.95.B0.E6.8F.90.E5.8D.87" title="c/language/conversion">默认参数提升</a>后的参数不拥有对应转换指定符所期待的类型，或若参数数量少于 <code>format</code> 的要求，则行为未定义。若有多于 <code>format</code> 要求的参数，则求值并忽略多出的参数。
</td></tr></table>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">返回值</span></h3>
<div class="t-li1"><span class="t-li">1,2)</span> 传输到输出流的字符数，或若出现输出错误或编码错误（对于字符串和字符转换指定符）则为负值。</div>
<div class="t-li1"><span class="t-li">3)</span> 写入到 <code>buffer</code> 的字符数（不计空终止字符），或若输出错误或编码错误（对于字符串和字符转换指定符）发生则为负值。</div>
<div class="t-li1"><span class="t-li">4)</span> 假如忽略 <code>bufsz</code> 则本应写入到 <code>buffer</code> 的字符数（不计空终止字符），或若出现输出错误或编码错误（对于字符串和字符转换指定符）则为负值。</div>
<div class="t-li1"><span class="t-li">5,6)</span> 传输到输出流的字符数，或若出现输出错误、运行时制约违规错误或编码错误则为负值。</div>
<div class="t-li1"><span class="t-li">7)</span> 写入到<code>buffer</code>的字符数，不计空终止字符（只要 <code>buffer</code> 不是空指针且 <code>bufsz</code> 非零且不大于 <code>RSIZE_MAX</code> ，就写入它），在运行时制约违规时为零，而在编码错误时为负值。</div>
<div class="t-li1"><span class="t-li">8)</span> 假如忽略 <code>bufsz</code> 则本应写入 <code>buffer</code> 的字符数的，不包含空终止字符（只要 <code>buffer</code> 不是空指针而 <code>bufsz</code> 非零且不大于 <code>RSIZE_MAX</code> ，就写入它），或若出现输出错误、运行时制约违规错误或编码错误则为负值。</div>
<h3><span class="mw-headline" id=".E6.B3.A8.E6.84.8F">注意</span></h3>
<p>C 标准及<a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html">POSIX</a> 指定 <code>sprintf</code> 及其变体的行为在参数与目标缓冲区重叠时未定义。示例：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1">sprintf<span class="br0">(</span>dst, <span class="st0">"%s and %s"</span>, dst, t<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// &lt;- 有错：未定义行为</span></pre></div></div>
<p><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html">POSIX 规定</a>在错误时设置 <span class="t-lc"><a href="../error/errno.html" title="c/error/errno">errno</a></span> 。它亦指定额外的转换指定，最值得注意的是对参数重排序的支持（紧随 <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">%</span></span></span> 后的 <span class="t-c"><span class="mw-geshi c source-c">n$</span></span> 指示第 <code>n</code> 个参数）。
</p><p>以零为 <code>bufsz</code> 和空指针为 <code>buffer</code> 调用 <code>snprintf</code> 可用于决定包含输出的缓冲区大小：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span>fmt <span class="sy1">=</span> <span class="st0">"sqrt(2) = %f"</span><span class="sy4">;</span>
<span class="kw4">int</span> sz <span class="sy1">=</span> snprintf<span class="br0">(</span><a href="../types/NULL.html"><span class="kw103">NULL</span></a>, <span class="nu0">0</span>, fmt, <a href="../numeric/math/sqrt.html"><span class="kw665">sqrt</span></a><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">char</span> buf<span class="br0">[</span>sz <span class="sy2">+</span> <span class="nu0">1</span><span class="br0">]</span><span class="sy4">;</span> <span class="co1">// 注意为终止空字符 +1</span>
snprintf<span class="br0">(</span>buf, <span class="kw4">sizeof</span> buf, fmt, <a href="../numeric/math/sqrt.html"><span class="kw665">sqrt</span></a><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span></pre></div></div>
<p>同 <code>snprintf</code> ，但不同于 <code>sprintf_s</code> ， <code>snprintf_s</code> 会将输出截断在 <code>bufsz-1</code> 之内。
</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co2">#include &lt;stdio.h&gt;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
    printf<span class="br0">(</span><span class="st0">"Strings:<span class="es1">\n</span>"</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">const</span> <span class="kw4">char</span><span class="sy2">*</span> s <span class="sy1">=</span> <span class="st0">"Hello"</span><span class="sy4">;</span>
    printf<span class="br0">(</span><span class="st0">"<span class="es1">\t</span>.%10s.<span class="es1">\n</span><span class="es1">\t</span>.%-10s.<span class="es1">\n</span><span class="es1">\t</span>.%*s.<span class="es1">\n</span>"</span>, s, s, <span class="nu0">10</span>, s<span class="br0">)</span><span class="sy4">;</span>
 
    printf<span class="br0">(</span><span class="st0">"Characters:<span class="es1">\t</span>%c %%<span class="es1">\n</span>"</span>, <span class="nu0">65</span><span class="br0">)</span><span class="sy4">;</span>
 
    printf<span class="br0">(</span><span class="st0">"Integers<span class="es1">\n</span>"</span><span class="br0">)</span><span class="sy4">;</span>
    printf<span class="br0">(</span><span class="st0">"Decimal:<span class="es1">\t</span>%i %d %.6i %i %.0i %+i %u<span class="es1">\n</span>"</span>, <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">4</span>, <span class="sy2">-</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>
    printf<span class="br0">(</span><span class="st0">"Hexadecimal:<span class="es1">\t</span>%x %x %X %#x<span class="es1">\n</span>"</span>, <span class="nu0">5</span>, <span class="nu0">10</span>, <span class="nu0">10</span>, <span class="nu0">6</span><span class="br0">)</span><span class="sy4">;</span>
    printf<span class="br0">(</span><span class="st0">"Octal:<span class="es1">\t</span>%o %#o %#o<span class="es1">\n</span>"</span>, <span class="nu0">10</span>, <span class="nu0">10</span>, <span class="nu0">4</span><span class="br0">)</span><span class="sy4">;</span>
 
    printf<span class="br0">(</span><span class="st0">"Floating point<span class="es1">\n</span>"</span><span class="br0">)</span><span class="sy4">;</span>
    printf<span class="br0">(</span><span class="st0">"Rounding:<span class="es1">\t</span>%f %.0f %.32f<span class="es1">\n</span>"</span>, <span class="nu16">1.5</span>, <span class="nu16">1.5</span>, <span class="nu16">1.3</span><span class="br0">)</span><span class="sy4">;</span>
    printf<span class="br0">(</span><span class="st0">"Padding:<span class="es1">\t</span>%05.2f %.2f %5.2f<span class="es1">\n</span>"</span>, <span class="nu16">1.5</span>, <span class="nu16">1.5</span>, <span class="nu16">1.5</span><span class="br0">)</span><span class="sy4">;</span>
    printf<span class="br0">(</span><span class="st0">"Scientific:<span class="es1">\t</span>%E %e<span class="es1">\n</span>"</span>, <span class="nu16">1.5</span>, <span class="nu16">1.5</span><span class="br0">)</span><span class="sy4">;</span>
    printf<span class="br0">(</span><span class="st0">"Hexadecimal:<span class="es1">\t</span>%a %A<span class="es1">\n</span>"</span>, <span class="nu16">1.5</span>, <span class="nu16">1.5</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">Strings:
    .     Hello.
    .Hello     .
    .     Hello.
Characters:     A %
Integers
Decimal:        1 2 000003 0  +4 4294967295
Hexadecimal:    5 a A 0x6
Octal:          12 012 04
Floating point
Rounding:       1.500000 2 1.30000000000000004440892098500626
Padding:        01.50 1.50  1.50
Scientific:     1.500000E+00 1.500000e+00
Hexadecimal:    0x1.8p+0 0X1.8P+0</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
<div class="t-ref-std-11">
<ul><li> C11 standard (ISO/IEC 9899:2011): 
</li></ul>
<dl><dd><ul><li> 7.21.6.1 The fprintf function (p: 309-316)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 7.21.6.3 The printf function (p: 324)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 7.21.6.5 The snprintf function (p: 325)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 7.21.6.6 The sprintf function (p: 325-326)
</li></ul>
</dd></dl>
<dl><dd><ul><li> K.3.5.3.1 The fprintf_s function (p: 591)
</li></ul>
</dd></dl>
<dl><dd><ul><li> K.3.5.3.3 The printf_s function (p: 593-594)
</li></ul>
</dd></dl>
<dl><dd><ul><li> K.3.5.3.5 The snprintf_s function (p: 594-595)
</li></ul>
</dd></dl>
<dl><dd><ul><li> K.3.5.3.6 The sprintf_s function (p: 595-596)
</li></ul>
</dd></dl>
<div class="t-ref-std-c99">
<ul><li> C99 standard (ISO/IEC 9899:1999): 
</li></ul>
<dl><dd><ul><li> 7.19.6.1 The fprintf function (p: 274-282)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 7.19.6.3 The printf function (p: 290)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 7.19.6.5 The snprintf function (p: 290-291)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 7.19.6.6 The sprintf function (p: 291)
</li></ul>
</dd></dl>
<div class="t-ref-std-c89">
<ul><li> C89/C90 standard (ISO/IEC 9899:1990): 
</li></ul>
<dl><dd><ul><li> 4.9.6.1 The fprintf function 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 4.9.6.3 The printf function 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 4.9.6.5 The sprintf function 
</li></ul>
</dd></dl>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="fwprintf.html" title="c/io/fwprintf"> <span class="t-lines"><span>wprintf</span><span>fwprintf</span><span>swprintf</span><span>wprintf_s</span><span>fwprintf_s</span><span>swprintf_s</span><span>snwprintf_s</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div>
</td>
<td>  打印格式化宽字符输出到 <span class="t-lc"><a href="../io.html" title="c/io">stdout</a></span> 、文件流或缓冲区 <br> <span class="t-mark">(函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="vfprintf.html" title="c/io/vfprintf"> <span class="t-lines"><span>vprintf</span><span>vfprintf</span><span>vsprintf</span><span>vsnprintf</span><span>vprintf_s</span><span>vfprintf_s</span><span>vsprintf_s</span><span>vsnprintf_s</span></span></a></div><div><span class="t-lines"><span></span><span></span><span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div>
</td>
<td>   打印格式化输出到<span class="t-lc"><a href="../io.html" title="c/io">stdout</a></span>、文件流或缓冲区<br>使用可变参数列表  <br> <span class="t-mark">(函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="fputs.html" title="c/io/fputs"> <span class="t-lines"><span>fputs</span></span></a></div></div>
</td>
<td>   将一个字符串写入文件流 <br> <span class="t-mark">(函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="fscanf.html" title="c/io/fscanf"> <span class="t-lines"><span>scanf</span><span>fscanf</span><span>sscanf</span><span>scanf_s</span><span>fscanf_s</span><span>sscanf_s</span></span></a></div><div><span class="t-lines"><span></span><span></span><span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div>
</td>
<td>   从<span class="t-lc"><a href="../io.html" title="c/io">stdin</a></span>、文件流或缓冲区读取格式化输入 <br> <span class="t-mark">(函数)</span> </td></tr>

</table></div>
</div>

<!-- 
NewPP limit report
Preprocessor visited node count: 9288/1000000
Preprocessor generated node count: 14397/1000000
Post‐expand include size: 256696/2097152 bytes
Template argument size: 68832/2097152 bytes
Highest expansion depth: 20/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:972-0!*!0!!zh!*!zh!* and timestamp 20200216161009 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=c/io/fprintf&amp;oldid=54105">https://zh.cppreference.com/mwiki/index.php?title=c/io/fprintf&amp;oldid=54105</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/c/io/fprintf">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2018年4月5日 (星期四) 02:38。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过19,937次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 2.490 secs. -->
	</body>
<!-- Cached 20200216161009 -->
</html>